
#ifndef Velyn_RockManager_h
#define Velyn_RockManager_h

// #include "Rock.h"
#include "RockGenerator.h"

namespace Velyn {

    class RockManager {

    private:
      
        vector<Rock*> rocks;
        
        RockGenerator rockGenerator;
        
        // const char* material;
        // const char* texture;
        
        void generateRocks() {
            
            // Rock* rock = new Rock("default", "/Users/n7down/Source/petra/textures/8.tga");
            Rock* rock = new Rock("/Users/n7down/Source/petra/textures/8.tga", "default");
            
            rock->sperical(2);
            rock->slice(4);
            // rock->smooth(2);
            rock->noise(2);
            
            rocks.push_back(rock);
        }

        // vector<Rock*>::iterator rockIterator;
        
        void deleteRocks() {
            vector<Rock*>::iterator rockIterator;
            
            for(rockIterator = rocks.begin(); rockIterator != rocks.end(); rockIterator++) {
                delete *rockIterator;
            }
        }
        
        int frameCounter;
        
        // bool _animationToggle;
        
    public:

        RockManager() : frameCounter(0) {
            generateRocks();
        }
        
        ~RockManager() {
            deleteRocks();
        }
        
        /* void animationToggle() {
            if(_animationToggle == true) {
                _animationToggle = false;
            }
            
            else {
                _animationToggle = true;
            }
        } */
        
        void add(Rock* rock) {
            rocks.push_back(rock);
        }
        
        void sample() {
            rocks.clear();
            generateRocks();
        }

        void render() {
            
            // if there are at least one rock in the container this is so i dont 
            // get a run time error for not having anything for the renderer
            /* if(rocks.size() > 0) {
                
                // float position = -1.5f;
                vector<Rock*>::iterator rockIterator;
                
                for(rockIterator = rocks.begin(); rockIterator != rocks.end(); rockIterator++) {
        
                    // glPushMatrix();
                    // glTranslatef(position, 0.0f, 0.0f);
                    
                    (*rockIterator)->render();
                    // glPopMatrix();
                    
                    // position += 3.0f;
                }
            } */
            
            // rocks[0]->bindTexture();
            // rocks[0]->useMaterial();
            // change to rocks[0]->renderMesh();
            rocks[0]->render();
            // rocks[0]->unbindTexture();
        }
        
        void animate() {
            
            frameCounter += 1;
                
            cout << "Frame: " << frameCounter << endl;
            
            int frameInterval = 10;
            
            if(frameCounter == frameInterval * 6) {
                rocks[0]->sperical(2);
                
                // for(int i = 0; i < 2; i++) {
                //    rocks[0]->subdivide();
                // }
            }
            
            if(frameCounter == frameInterval * 12) {
                rocks[0]->slice(5);
            }
            
            if(frameCounter == frameInterval * 18) {
                rocks[0]->noise(3);
            }
            
            if(frameCounter > frameInterval * 30) {
                rocks[0]->reset();
                frameCounter = 0;
            }
            
        }
        
        void useTextures() {
            glEnable(GL_TEXTURE_2D);
            // glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
            tgaLoadAndBind((char*)"/Users/n7down/Source/Velyn/textures/8.tga", NULL);
        }
        
        void useMaterials() {
            MaterialManager::getInstance()->apply("default");
        }
        
        // !!!: this does work
        // rockManager.enable(RockManager::TEXTURES);
        
        enum Capability { TEXTURES, MATERIALS };
        
        void enable(Capability capability) {
            switch(capability) {
                case MATERIALS: {
                    MaterialManager::getInstance()->apply("giberish");
                } break;
                
                case TEXTURES: {
                    glEnable(GL_TEXTURE_2D);
                    glPixelStorei( GL_UNPACK_ALIGNMENT, 1);
                    // tgaLoadAndBind((char*)texture, TGA_FREE);                
                    // glEnable(GL_CULL_FACE);
                } break;
                
                default: {
                
                } break;
            }
        }
    };
}

#endif
